<?php

$citizens = array();

$data_file = 'data.dat';

$lines = file($data_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($lines as $key => $line) {

	$a = unserialize($line);
	
	$gold = $a['raw3']*10 + $a['raw5']*35
					+ $a['one']*10 + $a['two']*30 + $a['three']*80 + $a['four']*180 + $a['five']*380
					+ $a['storage2']*35 + $a['storage3']*70;
	
	$currency = $a['raw1']*500 + $a['raw2']*2000 + $a['raw4']*7500 + $a['storage1']*750;
	
	// calc the amount of lands
	foreach ($a as $type => $num) {
		if ($type !== 'id' && $type !== 'name') { 
			$currency += 1000 * $num;
		}
	}
		
	$citizens[$a['name']] = array('gold' => $gold, 'currency' => $currency);
}

function to_gold($a)
{
	return $a / 400;
}

function cmp($a, $b)
{
	$total_a = $a['gold'] + to_gold($a['currency']);
	$total_b = $b['gold'] + to_gold($b['currency']);
	return $total_a < $total_b;
}

uasort($citizens, "cmp");

$i = 0;
foreach($citizens as $name => $value) {
	++$i;
	$s = sprintf("<$i> %.2f gold (%d cc), $name\r\n", $value['gold'] + to_gold($value['currency']), $value['currency']);
	echo $s;
}

?>